import { _decorator, AudioClip, Component, director, Node, NodeEventType, resources, Vec3 } from 'cc';
import { GLOBALUtil } from './GlobalUtil';
import { SoundManager } from './SoundManager';
const { ccclass, property } = _decorator;

@ccclass('VictoryManager')
export class VictoryManager extends Component {
    async start() {
        resources.load('Audios/PVZ/crazydaveshort1', AudioClip, (err, clip) => {
            if (err) {
                console.error(err);
                return;
            }
            const soundManager = SoundManager.getInstance();
            soundManager.stopBGM();
            setTimeout(() => {
                soundManager.playSFX(clip);
            }, 100);
        })
        const node: Node = await GLOBALUtil.LoadMessage(false, '没想到你居然能通关😍\n(点我继续...)', this.node, new Vec3(60, 800, 0), true, 0, new Vec3(-300, 800, 0)) as Node;
        node.once(NodeEventType.NODE_DESTROYED, async () => {
            resources.load('Audios/PVZ/crazydaveshort2', AudioClip, (err, clip) => {
                if (err) {
                    console.error(err);
                    return;
                }
                const soundManager = SoundManager.getInstance();
                soundManager.playSFX(clip);
            })
            const node: Node = await GLOBALUtil.LoadMessage(false, '但是就算通关了,\n也没有什么奖励😏\n(点我继续...)', this.node, new Vec3(60, 800, 0), true, 0, new Vec3(-300, 800, 0)) as Node;
            node.once(NodeEventType.NODE_DESTROYED, async () => {
                resources.load('Audios/PVZ/crazydaveshort3', AudioClip, (err, clip) => {
                    if (err) {
                        console.error(err);
                        return;
                    }
                    const soundManager = SoundManager.getInstance();
                    soundManager.playSFX(clip);
                })
                const node: Node = await GLOBALUtil.LoadMessage(false, '作者是个臭打游戏的🥱\n(点我继续...)', this.node, new Vec3(60, 800, 0), true, 1, new Vec3(-300, 800, 0)) as Node;
                node.once(NodeEventType.NODE_DESTROYED, async () => {
                    resources.load('Audios/PVZ/crazydaveshort2', AudioClip, (err, clip) => {
                        if (err) {
                            console.error(err);
                            return;
                        }
                        const soundManager = SoundManager.getInstance();
                        soundManager.playSFX(clip);
                    })
                    resources.load('Audios/Miku', AudioClip, (err, clip) => {
                        if (err) {
                            console.error(err);
                            return;
                        }
                        const soundManager = SoundManager.getInstance();
                        const muteState = soundManager.getBGMMuteStatus();
                        if (muteState) {
                            soundManager.toggleBGMMute();
                        }
                        soundManager.playBGM(clip, 0.7);
                    })
                    const node: Node = await GLOBALUtil.LoadMessage(false, '也是个Miku的粉丝😃\n那就放首Miku的歌吧😄', this.node, new Vec3(60, 800, 0), true, 2, new Vec3(-300, 800, 0)) as Node;
                    node.once(NodeEventType.NODE_DESTROYED, async () => {
                        let time = 39;
                        const node: Node = await GLOBALUtil.LoadMessage(false, `请最后欣赏音乐${time}秒`, this.node, new Vec3(60, 800, 0), true, 3, new Vec3(-300, 800, 0)) as Node;
                        this.schedule(async () => {
                            if (time >= 1) {
                                time -= 1;
                                const node: Node = await GLOBALUtil.LoadMessage(false, `请最后欣赏音乐${time}秒`, this.node, new Vec3(60, 800, 0), true, 3, new Vec3(-300, 800, 0)) as Node;
                            } else if(time <= 0) {
                                this.unscheduleAllCallbacks();
                                //更新胜利成就
                                GLOBALUtil.updateVictory();
                                resources.load('Audios/PVZ/crazydaveshort1', AudioClip, (err, clip) => {
                                    if (err) {
                                        console.error(err);
                                        return;
                                    }
                                    const soundManager = SoundManager.getInstance();
                                    soundManager.playSFX(clip);
                                })
                                const lastNode: Node = await GLOBALUtil.LoadMessage(false, '获得称号-MikuFans😃\n可在个人页面查看\n(点我返回主页)', this.node, new Vec3(60, 800, 0), true, 3, new Vec3(-300, 800, 0)) as Node;
                                lastNode.once(NodeEventType.NODE_DESTROYED, async () => {
                                    director.loadScene("MenuScene");
                                })
                            };
                        }, 1)
                    })
                })
            })
        })
    }

    update(deltaTime: number) {
        
    }
    
}


